use pid file_yr dmsl file_mth uic  ap_m_hfp typla strength_cat_cd grade using "${rawdataorig}/ace_panel_v3_unit_deploy_nopii", clear 
keep if typla == "HF"  
g file_myr = ym(file_yr,file_mth)
format file_myr %tm
sort pid file_myr
g ap_m_hfp_orig = ap_m_hfp
replace ap_m_hfp = 0 if ap_m_hfp ==. | ap_m_hfp < 0
g any_hfp = ap_m_hfp > 0 
g uic_dmsl_merge_date_ym = file_myr
rename dmsl dmsl_orig
merge m:1 uic uic_dmsl_merge_date_ym using "raw_dta/clean_uic_to_dmsl", keep(1 3) nogen
merge m:1 dmsl file_yr file_mth using "raw_dta/time_cons_dmsl.dta", keep(1 3) nogen
replace dmsl_tc = dmsl if dmsl_tc == "" 
replace dmsl_tc = "missing" if dmsl_tc == ""			
g temp1 = strength_cat_cd == "OPR"
bys pid (file_myr): egen m1_ = min(file_myr) if temp1 == 1
drop temp1 
bys pid (file_myr): egen date_of_arrival_1 = max(m1_) 
drop m1_ 

g firstpost1 = date_of_arrival_1 == file_myr
g dur1_ = firstpost1
bys pid (file_myr): replace dur1_ = 1 if dmsl_tc == dmsl_tc[_n-1] & dur1_[_n-1] == 1 & file_myr == file_myr[_n-1]+1
bys pid (file_myr): egen duration_1 = total(dur1_)
rename dur1_ dummy_still_at_dmsl_1
bys pid (file_myr): egen any_hfp_firstpost = max(any_hfp*dummy_still_at_dmsl_1) 
bys pid (file_myr): egen months_hfp_firstpost = total(any_hfp*dummy_still_at_dmsl_1) 

keep pid dmsl_tc file_myr firstpost1 date_of_arrival_1 dummy_still_at_dmsl_1 duration_1 ap_m_hfp any_hfp any_hfp_firstpost months_hfp_firstpost
format date_of_arrival_1 %tm

drop if date_of_arrival == . 

g msince = file_myr - date_of_arrival_1
keep if msince >= -2 & msince <= 36

tsset pid msince 
tsfill, full

	bys pid (msince): replace file_myr = file_myr[_n+1] -1 if file_myr ==. & msince == -1 
	bys pid (msince): replace date_of_arrival_1 = date_of_arrival_1[_n+1] if date_of_arrival_1 ==. & msince == -1
	bys pid (msince): replace any_hfp_firstpost = any_hfp_firstpost[_n+1] if any_hfp_firstpost ==. & msince == -1
	bys pid (msince): replace months_hfp_firstpost = months_hfp_firstpost[_n+1] if months_hfp_firstpost ==. & msince == -1
	bys pid (msince): replace duration_1 = duration_1[_n+1] if duration_1 ==. & msince == -1 		
	bys pid (msince): replace firstpost1 = 0 if firstpost1 ==. & msince == -1 
	bys pid (msince): replace dummy_still_at_dmsl_1 = 0 if dummy_still_at_dmsl_1 ==. & msince == -1 
	bys pid (msince): replace any_hfp = 0 if any_hfp ==. & msince == -1 
	
	bys pid (msince): replace file_myr = file_myr[_n+1] -1 if file_myr ==. & msince == -2
	bys pid (msince): replace date_of_arrival_1 = date_of_arrival_1[_n+1] if date_of_arrival_1 ==. & msince == -2 
	bys pid (msince): replace any_hfp_firstpost = any_hfp_firstpost[_n+1] if any_hfp_firstpost ==. & msince == -2
	bys pid (msince): replace months_hfp_firstpost = months_hfp_firstpost[_n+1] if months_hfp_firstpost ==. & msince == -2	
	bys pid (msince): replace duration_1 = duration_1[_n+1] if duration_1 ==. & msince == -2	
	bys pid (msince): replace firstpost1 = 0 if firstpost1 ==. & msince == -2
	bys pid (msince): replace dummy_still_at_dmsl_1 = 0 if dummy_still_at_dmsl_1 ==. & msince == -2
	bys pid (msince): replace any_hfp = 0 if any_hfp ==. & msince == -2

	bys pid (msince): replace file_myr = file_myr[_n-1]+1 if file_myr ==. 
	bys pid (msince): replace date_of_arrival_1 = date_of_arrival_1[_n-1] if date_of_arrival_1 ==.
	bys pid (msince): replace any_hfp_firstpost = any_hfp_firstpost[_n-1] if any_hfp_firstpost ==. 
	bys pid (msince): replace months_hfp_firstpost = months_hfp_firstpost[_n-1] if months_hfp_firstpost ==. 	
	bys pid (msince): replace duration_1 = duration_1[_n-1] if duration_1 ==. 		
	bys pid (msince): replace firstpost1 = 0 if firstpost1 ==. 
	bys pid (msince): replace dummy_still_at_dmsl_1 = 0 if dummy_still_at_dmsl_1 ==. 
	bys pid (msince): replace any_hfp = 0 if any_hfp ==. 

g dmsl_tc_first = dmsl_tc if firstpost1 
bys pid (msince): replace dmsl_tc_first = dmsl_tc_first[_n+1] if msince == -1
bys pid (msince): replace dmsl_tc_first = dmsl_tc_first[_n+1] if msince == -2
bys pid (msince): replace dmsl_tc_first = dmsl_tc_first[_n-1] if dmsl_tc_first == ""
replace any_hfp = 0 if dummy_still_at_dmsl_1 == 0
rename any_hfp any_hfp_at_dmsl

keep pid msince dmsl_tc_first file_myr date_of_arrival_1 dummy_still_at_dmsl_1 duration_1 any_hfp_at_dmsl any_hfp_firstpost months_hfp_firstpost 
tsset pid msince 

g dmsl_tc = dmsl_tc_first
merge m:1 file_myr dmsl_tc using "raw_dta/share_dep_by_dmsl_tc_month_whole_samp_v2", keep(1 3)
tab _merge
drop _merge
rename any_hfp shr_hfp 

g shr_hfp_loo = (count_enlisted*shr_hfp - any_hfp_at_dmsl)/(count_enlisted-dummy_still_at_dmsl_1)
drop dmsl_tc
sort pid msince 

compress
save "created_dta/firstterm_depinstrument_dmsl_long_whole_samp_v2", replace 

keep pid file_myr date_of_ar dummy_still msince dmsl_tc_first duration_1 count_enlisted shr_hfp_loo any_hfp_firstpost months_hfp_firstpost
tostring(msince), replace
replace msince = "m1" if msince == "-1"
replace msince = "m2" if msince == "-2"
rename dummy_still_at_dmsl_1 dummy_still_at_dmsl_
rename duration_1 duration_
rename file_myr file_myr_
rename  count_enlisted count_enlisted_
rename shr_hfp_loo shr_hfp_loo_
rename any_hfp_firstpost any_hfp_firstpost_
rename months_hfp_firstpost months_hfp_firstpost_
reshape wide file_myr dummy_still_at_dmsl  count_enli shr_hfp_loo duration_ any_hfp_firstpost_ months_hfp_firstpost_, i(pid) j(msince) string
order pid date_of_ar dmsl_tc_first *m2 *m1
g durationat_firstpost = duration_0 
g any_hfp_firstpost = any_hfp_firstpost_0 
g months_hfp_firstpost = months_hfp_firstpost_0
drop duration_* 
drop  any_hfp_firstpost_*
drop  months_hfp_firstpost_*
compress 
save "created_dta/firstterm_depinstrument_dmsl_wide_whole_samp_v2", replace 

	use pid pmos dmsl yos arloc file_yr file_mth uic  ap_m_hfp typla strength_cat_cd grade using "${rawdataorig}/ace_panel_v3_unit_deploy_nopii", clear 
	
		g file_myr = ym(file_yr,file_mth)
		format file_myr %tm
		sort pid file_myr
		
		g ap_m_hfp_orig = ap_m_hfp
		replace ap_m_hfp = 0 if ap_m_hfp ==. | ap_m_hfp < 0
		g any_hfp = ap_m_hfp > 0 

		bys pid (file_myr):  egen any_hfp_ever = max(any_hfp)
		bys pid (file_myr):  egen months_hfp_ever = total(any_hfp)
		
		bys pid (file_myr):  egen first_obs_myr = min(file_myr)
		
		bys pid (file_myr):  egen last_obs_myr = max(file_myr)

		bys pid (file_myr): gen entry_grade = grade if _n == 1
		bys pid (file_myr): replace entry_grade = entry_grade[_n-1] if entry_grade==""
		
		bys pid (file_myr): gen entry_yos = yos if _n == 1
		bys pid (file_myr): replace entry_yos = entry_yos[_n-1] if entry_yos==.
				

		bys pid (file_myr): gen entry_pmos = pmos if _n == 1
		bys pid (file_myr): replace entry_pmos = entry_pmos[_n-1] if entry_pmos==""
		
		
		bys pid (file_myr): gen entry_arloc = arloc if _n == 1
		bys pid (file_myr): replace entry_arloc = entry_arloc[_n-1] if entry_arloc==""
	
	
	keep if typla == "HF"
	g uic_dmsl_merge_date_ym = file_myr
	rename dmsl dmsl_orig
	merge m:1 uic uic_dmsl_merge_date_ym using "raw_dta/clean_uic_to_dmsl", keep(1 3) nogen
	merge m:1 dmsl file_yr file_mth using "raw_dta/time_cons_dmsl.dta", keep(1 3) nogen
	replace dmsl_tc = dmsl if dmsl_tc == ""  
	replace dmsl_tc = "missing" if dmsl_tc == ""

	bys pid (file_myr): g temp1 = strength_cat_cd == "OPR"

	bys pid (file_myr): egen m1_ = min(file_myr) if temp1 == 1
	bys pid (file_myr): egen m1 = max(m1_) 
	drop  m1_
	g firstpost1 = m1 == file_myr	
	rename m1 date_of_arrival_local
	g dur1_ = firstpost1

	
	bys pid (file_myr): replace dur1_ = 1 if dmsl_tc == dmsl_tc[_n-1] & dur1_[_n-1] == 1  & file_myr == file_myr[_n-1]+1
	bys pid (file_myr): egen dur1 = total(dur1_)
	rename dur1_ dummy_still_at_dmsl_1
	rename dur1 duration_at_first_post_chk
	bys pid (file_myr): egen any_hfp_firstpost_chk = max(any_hfp*dummy_still_at_dmsl_1) 
	bys pid (file_myr): egen months_hfp_firstpost_chk = total(any_hfp*dummy_still_at_dmsl_1) 

		preserve
		keep pid file_myr arloc orgraa dummy_still_at_dmsl_1
		keep if dummy_still_at_dmsl_1 == 1
		sort pid file_myr 
		bys pid (file_myr): keep if _n == 1
		keep pid arloc orgraa
		rename arloc firstpost_arloc_ind
		rename orgraa firstpost_orgraa_ind
		tempfile arloc
		save `arloc', replace
		restore
		merge m:1 pid using `arloc', keep(1 3) nogen
	
	keep pid  date_of_arrival_local duration_at_first_post_chk any_hfp_firstpost_chk months_hfp_firstpost_chk firstpost_arloc_ind firstpost_orgraa_ind entry_grade entry_yos entry_arloc entry_pmos any_hfp_ever months_hfp_ever first_obs_myr last_obs_myr
	duplicates drop
	tempfile depinfo
	save `depinfo', replace
	
use "created_dta/firstterm_depinstrument_dmsl_wide_whole_samp_v2", clear
merge 1:1 pid using `depinfo', keep(1 3) nogen
format date_of_arrival_local %tm

preserve
use pid filedt_s afqsc arloc civedcatd civedcatg grade hgt wgt marst nrdep nrdepa nrdepc phyca pmos ///
race race9 sex statehor terms yos  ///
acddt_s arrdt_s entrydt_s datla_s dlpcs_s etsdt_s sepdt_s dor_s dobym_s ///
ap_m_hfp arloc conap orsap* asgcons using "${rawdataorig}/ace_panel_v3_unit_deploy_nopii_first", clear
foreach var of varlist filedt_s afqsc arloc civedcatd civedcatg grade hgt wgt marst nrdep nrdepa nrdepc phyca pmos ///
race race9 sex statehor terms yos  ///
acddt_s arrdt_s entrydt_s datla_s dlpcs_s etsdt_s sepdt_s dor_s dobym_s ///
ap_m_hfp conap orsap* asgcons {
	rename `var' init_`var'
}
tempfile init_demo
save `init_demo', replace
restore

merge 1:1 pid using `init_demo', keep(1 3) nogen

count if regexm(dmsl_tc_first,"_TC")
count if regexm(dmsl_tc_first,"_TC") == 0 & dmsl_tc_first!="missing"
count if dmsl_tc_first == "missing"


	order dummy_*m1 dummy_*m2 dummy_*sl_0 dummy_*sl_1 ///
	dummy_*sl_2 dummy_*sl_3 dummy_*sl_4 dummy_*sl_5 dummy_*sl_6 dummy_*sl_7 dummy_*sl_8 dummy_*sl_9 ///
	dummy_*sl_1* dummy_*sl_2* dummy_*sl_3*


*PERFORM CHECKS AND DROP UNNESCESSARY VARIABLES 
g chk =   date_of_arrival_1  ==   date_of_arrival_local
tab chk, m
drop chk
g chk =    durationat_firstpost    ==    duration_at_first_post_chk 
tab chk, m
drop chk
g chk =    any_hfp_firstpost    ==    any_hfp_firstpost_chk  
tab chk, m
drop chk
g chk =    months_hfp_firstpost     ==    months_hfp_firstpost_chk   
tab chk, m
drop chk

g chk =    entry_yos     ==    init_yos   
tab chk, m
drop chk
g chk =    entry_pmos     ==    init_pmos 
tab chk, m
drop chk
g chk =    entry_grade     ==    init_grade
tab chk, m
drop chk
g chk =    entry_arloc     ==    init_arloc
tab chk, m
drop chk

drop date_of_arrival_local duration_at_first_post_chk any_hfp_firstpost_chk months_hfp_firstpost_chk ///
entry_yos entry_pmos entry_grade entry_arloc


egen chk = rowtotal(dummy*) 
g ok = chk == durationat 
tab ok, m 
tab ok if durationat <= 36, m
drop ok chk

preserve 
keep pid date_of_arrival_1 
tempfile pids 
save `pids', replace

use pid file_yr file_mth ap_m_hfp using "${rawdataorig}/ace_panel_v3_unit_deploy_nopii", clear 
merge m:1 pid using `pids', keep(3) nogen
g ym = ym(file_yr,file_mth)
g start = date_of_arrival_1 == ym  
sort pid ym
replace ap_m_hfp = 0 if ap_m_hfp ==. | ap_m_hfp < 0
g any_hfp = ap_m_hfp > 0 

by pid: g deployed_m1 = any_hfp == 1 & start[_n+1] == 1 
by pid: g deployed_m2 = any_hfp == 1 & start[_n+2] == 1 

forvalues x = 0/108 {
	by pid: g deployed_`x' = any_hfp == 1 & start[_n-`x'] == 1  
}

collapse (max) deployed_*, by(pid)
save "created_dta/dep_info_whole_samp_v2.dta", replace 


 use pid filedt_s using "${rawdataorig}/ace_panel_v3_unit_deploy_nopii_last", clear 
 merge 1:1 pid using `pids',  keep(3) nogen
 g last_ym = ym(year(file),month(file))
 keep pid last_ym
 save "created_dta/lastym_info_whole_samp.dta", replace 

restore

merge 1:1 pid using "created_dta/dep_info_whole_samp_v2.dta", nogen keep(1 3)
merge 1:1 pid using "created_dta/lastym_info_whole_samp.dta", nogen keep(1 3)

g dep_win_1yr = (deployed_0 == 1 | deployed_1 == 1 | deployed_2 == 1  | deployed_3 == 1 /// 
| deployed_4 == 1 | deployed_5 == 1 | deployed_6 == 1  | deployed_7 == 1 /// 
| deployed_8 == 1 | deployed_9 == 1 | deployed_10 == 1  | deployed_11 == 1)

g dep_win_2yr = (deployed_0 == 1 | deployed_1 == 1 | deployed_2 == 1  | deployed_3 == 1 /// 
| deployed_4 == 1 | deployed_5 == 1 | deployed_6 == 1  | deployed_7 == 1 /// 
| deployed_8 == 1 | deployed_9 == 1 | deployed_10 == 1  | deployed_11 == 1 ///
| deployed_12 == 1 | deployed_13 == 1 | deployed_14 == 1  | deployed_15 == 1  ///
| deployed_16 == 1 | deployed_17 == 1 | deployed_18 == 1  | deployed_19 == 1  ///
| deployed_20 == 1 | deployed_21 == 1 | deployed_22 == 1  | deployed_23 == 1) 

g dep_win_3yr = (deployed_0 == 1 | deployed_1 == 1 | deployed_2 == 1  | deployed_3 == 1 /// 
| deployed_4 == 1 | deployed_5 == 1 | deployed_6 == 1  | deployed_7 == 1 /// 
| deployed_8 == 1 | deployed_9 == 1 | deployed_10 == 1  | deployed_11 == 1 ///
| deployed_12 == 1 | deployed_13 == 1 | deployed_14 == 1  | deployed_15 == 1  ///
| deployed_16 == 1 | deployed_17 == 1 | deployed_18 == 1  | deployed_19 == 1  ///
| deployed_20 == 1 | deployed_21 == 1 | deployed_22 == 1  | deployed_23 == 1 ///
| deployed_24 == 1 | deployed_25 == 1 | deployed_26 == 1  | deployed_27 == 1  ///
| deployed_28 == 1 | deployed_29 == 1 | deployed_30 == 1  | deployed_31 == 1  ///
| deployed_32 == 1 | deployed_33 == 1 | deployed_34 == 1  | deployed_35 == 1 )

summ date_of_arrival_1, d
drop if date_of_arrival_1 == r(min)

compress
save "created_dta/fullsample_tcdmsl_withdemo_whole_samp_v3", replace
count

cap log close
